import unittest
import requests

#类  集成方法
class MyTestCase(unittest.TestCase):
    #方法
    def test_something(self):
        #发送get请求第一种方式
        # r=requests.get("https://selectcar.yiche.com/selectcarforapp?mid=9&s=4&page=1&pagesize=20&cityId=201",timeout=2)
        # print(r.json())
        #发送get请求的第二种方式
        cardata={"mid":9,"s":4,"page":1,"pagesize":20,"cityId":201}
        # head={"Content-Type":"application/x-www-form-urlencoded"} headers=head
        car=requests.get("https://selectcar.yiche.com/selectcarforapp",params=cardata,timeout=2)
        #timeout超时时间，代表这个接口发送到服务器必须2s内给我返回结果，如果返回不了报错
        #获取返回的json值
        # print(car.json()) ***
        #获取接口的url ***
        print(car.url)
        #获取这个接口响应状态码 ***
        # print(car.status_code)
        #将返回的值用文本输出 ***
        # print(car.text)
        #获取cookie  ***
        # print(car.cookies)
        #打印响应头信息 car.headers["Content-Type"] 获取具体某一个值
        # print(car.headers)
        #打印编码格式
        # print(car.encoding)
         # 基本判断这个接口是否通
        if car.status_code==200:
            ca=car.json()
            print(ca["status"])
            self.assertEqual(ca['status'],1)
            self.assertEqual(car.json()['message'], "ok")
            # self.assertEqual(ca['data']['resList'][0]['brandName'],"一汽-大众奥迪")
            # print(ca['data']['resList'][1]['tags'][1]['value'])
            carlen=len(ca['data']['resList'])
            for i in range(carlen):
                if ca['data']['resList'][i]['brandName'] =="":
                    raise RuntimeError("brandName不能为空")
                else:
                    pass
        else:
            raise RuntimeError(f"此接口状态异常:{car.status_code}，请排查")

#主方法
if __name__ == '__main__':
    unittest.main()
